package com.iflysse.controller;

import com.alibaba.fastjson.JSONObject;
import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.common.PageBeanBoResp;
import com.iflysse.pojo.bo.req.StudentIdBoReq;
import com.iflysse.pojo.bo.req.StudentIdPageBoReq;
import com.iflysse.pojo.bo.resp.CompleteTaskBoResp;
import com.iflysse.pojo.bo.resp.CompleteTaskListBoResp;
import com.iflysse.pojo.bo.resp.TaskBoResp;
import com.iflysse.pojo.bo.resp.TaskListBoResp;
import com.iflysse.pojo.vo.req.StudentIdVoReq;
import com.iflysse.pojo.vo.resp.*;
import com.iflysse.services.TaskService;
import com.iflysse.util.CommonUtil;
import com.iflysse.util.FileUpdateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/studentTask")
@Api(value = "学生端任务的接口")
@Log4j
public class StudentTaskController{

    @Autowired
   private TaskService taskService;
    /**
     * 学生首页展示,获取当前学生所有的已学习任务列表
     */
    @PostMapping("/queryCompleteTask")
    @ApiOperation("学生首页展示,获取当前学生所有的已学习任务列表")
    public TaskAllWaitVoResp queryCompleteTask(StudentIdVoReq studentIdVoReq) {

        TaskAllWaitVoResp taskAllWaitVoResp=new TaskAllWaitVoResp();

        try{

            log.info("学生首页展示,获取所有的已学习任务列表参数："+studentIdVoReq);


            StudentIdPageBoReq studentIdPageBoReq=  CommonUtil.objToObj(studentIdVoReq, StudentIdPageBoReq.class);
            studentIdPageBoReq.setStudentId(studentIdVoReq.getStudentId());
            studentIdPageBoReq.getPageBeanBoReq().setPageSize(studentIdVoReq.getPageSize());
            studentIdPageBoReq.getPageBeanBoReq().setPageIndex(studentIdVoReq.getPageIndex());
            log.info("学生首页展示,调用业务接口获取所有的已学习任务列表参数bean"+studentIdPageBoReq);

            PageBeanBoResp<CompleteTaskBoResp> pageBeanBoResp=   taskService.getCompleteTaskByStudentId(studentIdPageBoReq);

            PageBeanVoResp pageBeanVoResp=  CommonUtil.objToObj(pageBeanBoResp,PageBeanVoResp.class);

            taskAllWaitVoResp.setResult(pageBeanVoResp);
            taskAllWaitVoResp.setCode(ResultEnum.SUCCESS.getCode());
            taskAllWaitVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生首页展示,获取当前学生所有的已学习任务列表响应bean:"+taskAllWaitVoResp);

            return taskAllWaitVoResp;
        }catch (Exception e){
            log.error("学生首页展示,获取当前学生所有的已学习任务列表出现异常："+e);
            CommonUtil.printError(taskAllWaitVoResp);
            return taskAllWaitVoResp;
        }

    }





    /**
     *  学生首页展示,获取当前学生前4个已学习任务列表
     */
    @PostMapping("/queryTopFourCompleteTask")
    @ApiOperation("学生首页展示,获取当前学生所有的已学习任务列表")
    public CompleteTaskVoResp queryTopFourCompleteTask(StudentIdVoReq studentIdVoReq){

        CompleteTaskVoResp completeTaskVoResp=new CompleteTaskVoResp();

        try{
            log.info("学生首页展示,获取当前学生前4个已学习任务列表的参数："+studentIdVoReq);
            StudentIdBoReq studentIdBoReq= CommonUtil.objToObj(studentIdVoReq, StudentIdBoReq.class);
            log.info("调用业务接口获取当前学生前4个已学习任务列表的参数："+studentIdBoReq);

            CompleteTaskListBoResp completeTaskListBoResp= taskService.getTop4CompleteTaskByStudentId(studentIdBoReq);
            List<CompleteTaskResp> result=new ArrayList<>();
            completeTaskListBoResp.getCompleteTaskBoRespList().forEach(completeTaskBoResp -> {
                CompleteTaskResp completeTaskResp=  CommonUtil.objToObj(completeTaskBoResp,CompleteTaskResp.class);
                completeTaskResp.setPhotoPath(FileUpdateUtil.prefixResources+completeTaskBoResp.getPhotoPath());
                result.add(completeTaskResp);
            });

            completeTaskVoResp.setResult(result);
            completeTaskVoResp.setCode(ResultEnum.SUCCESS.getCode());
            completeTaskVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生首页展示,获取当前学生前4个已学习任务列表:"+completeTaskVoResp);

            return completeTaskVoResp;
        }catch (Exception e){
            log.error("学生首页展示,获取当前学生前4个已学习任务列表出现异常"+e);
            CommonUtil.printError(completeTaskVoResp);
            return completeTaskVoResp;
        }

    }





    /**
     * 学生首页展示,获取所有待学习任务列表
     */
    @PostMapping("/queryWaitingTask")
    @ApiOperation("学生首页展示,获取所有待学习任务列表")
    public TaskAllWaitVoResp queryWaitingTask(StudentIdVoReq studentIdVoReq){

        TaskAllWaitVoResp taskAllWaitVoResp=new TaskAllWaitVoResp();


        try{

            log.info("学生首页展示,获取所有的待学习任务列表参数："+studentIdVoReq);

            StudentIdPageBoReq studentIdPageBoReq=  CommonUtil.objToObj(studentIdVoReq, StudentIdPageBoReq.class);
            studentIdPageBoReq.setStudentId(studentIdVoReq.getStudentId());
            studentIdPageBoReq.getPageBeanBoReq().setPageSize(studentIdVoReq.getPageSize());
            studentIdPageBoReq.getPageBeanBoReq().setPageIndex(studentIdVoReq.getPageIndex());
            log.info("学生首页展示,调用业务接口获取所有的待学习任务列表参数bean"+studentIdPageBoReq);
            PageBeanBoResp<TaskBoResp> pageBeanBoResp= taskService.getWaitingTaskByStudentId(studentIdPageBoReq);

            PageBeanVoResp pageBeanVoResp=  CommonUtil.objToObj(pageBeanBoResp, PageBeanVoResp.class);
            List<TaskResp>taskResps=new ArrayList<>();

            pageBeanVoResp.getList().forEach(taskBoResp -> {
                String taskRespStr= JSONObject.toJSONString(taskBoResp);
                TaskResp taskResp=  JSONObject.parseObject(taskRespStr,TaskResp.class);
//                TaskResp taskResp= CommonUtil.objToObj(taskBoResp,TaskResp.class);
                taskResp.setPhotoPath(FileUpdateUtil.prefixResources+taskResp.getPhotoPath());
                try {
                    Date date= new Date(Long.valueOf(taskResp.getTaskExpirationDate()));
                    String d=CommonUtil.dateSourceToString(date,CommonUtil.pattern);
                    taskResp.setTaskExpirationDate(d);
                }catch (Exception e){
                    e.printStackTrace();
                }
                taskResps.add(taskResp);
            });
            pageBeanVoResp.setList(taskResps);


            taskAllWaitVoResp.setResult(pageBeanVoResp);

            taskAllWaitVoResp.setCode(ResultEnum.SUCCESS.getCode());
            taskAllWaitVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生首页展示,获取所有的待学习任务列表响应："+taskAllWaitVoResp);
            return taskAllWaitVoResp;
        }catch (Exception e){
            log.error("学生首页展示,获取所有的待学习任务列表出现异常"+e);
            CommonUtil.printError(taskAllWaitVoResp);
            return  taskAllWaitVoResp;
        }
    }




    /**
     * 学生首页展示,获取当前学生前4个待学习任务列表
     */
    @PostMapping("/queryTopFourWaitingTask")
    @ApiOperation("学生首页展示,获取当前学生前4个待学习任务列表")
    public TaskVoResp queryTopFourWaitingTask(StudentIdVoReq studentIdVoReq){

        TaskVoResp taskVoResp=new TaskVoResp();
        try{

            log.info("学生首页展示,获取当前学生前4个待学习任务列表的参数："+studentIdVoReq);
            StudentIdBoReq studentIdBoReq= CommonUtil.objToObj(studentIdVoReq, StudentIdBoReq.class);
            log.info("调用业务接口获取当前学生前4个待学习任务列表的参数："+studentIdBoReq);
            TaskListBoResp taskListBoResp= taskService.getTop4WaitingTaskByStudentId(studentIdBoReq);
            List<TaskResp> result=new ArrayList<TaskResp>();
            taskListBoResp.getTaskBoRespList().forEach(taskBoResp -> {
                TaskResp taskResp=   CommonUtil.objToObj(taskBoResp, TaskResp.class);
                //特殊字段处理
                taskResp.setPhotoPath(FileUpdateUtil.prefixResources+taskBoResp.getPhotoPath());
                taskResp.setTaskExpirationDate(CommonUtil.dateSourceToString(taskBoResp.getTaskExpirationDate(),CommonUtil.pattern));
                result.add(taskResp);
            });

            taskVoResp.setResult(result);
            taskVoResp.setCode(ResultEnum.SUCCESS.getCode());
            taskVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生首页展示,获取当前学生前4个待学习任务列表:"+taskVoResp);

            return taskVoResp;
        }catch (Exception e){
            log.error("学生首页展示,获取当前学生前4个待学习任务列表出现异常："+e);
            CommonUtil.printError(taskVoResp);
            return taskVoResp;
        }
    }

}
